﻿using Ormon.HostLinkLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace HostLinkTest
{
    internal class Program
    {
        static async Task Main(string[] args)
        {
            #region C-Mode方式

            HostLinkCmodeDevice plc1 = new HostLinkCmodeDevice("COM5", 115200, Parity.None, 8, StopBits.One);

            SemaphoreSlim semaphoreSlim_Comm1 = new SemaphoreSlim(1, 1);

            plc1.Connect();


            var Task1 = Task.Run(async () =>
            {
                await semaphoreSlim_Comm1.WaitAsync();

                Stopwatch stopwatch1 = new Stopwatch();

                stopwatch1.Start();

                string str1 = await plc1.TestCommandAsync("123123");

                stopwatch1.Stop();

                Console.WriteLine("TestCommand:");

                Console.WriteLine(str1);

                Console.WriteLine($"花费时间：{stopwatch1.ElapsedMilliseconds}ms");

                semaphoreSlim_Comm1.Release();

            });

            var Task2 = Task.Run(async () =>
            {
                await semaphoreSlim_Comm1.WaitAsync();

                Stopwatch stopwatch1 = new Stopwatch();

                stopwatch1.Start();

                string str1 = await plc1.Read_DMAreaAsync(0, 3);

                stopwatch1.Stop();

                Console.WriteLine("ReadDM:");

                Console.WriteLine(str1);

                Console.WriteLine($"花费时间：{stopwatch1.ElapsedMilliseconds}ms");

                semaphoreSlim_Comm1.Release();

            });

            await Task.WhenAll(Task1, Task2);

            plc1.DisConnect();

            semaphoreSlim_Comm1.Dispose();

            #endregion



            #region FINS-Mode方式

            //HostLinkDevice plc2 = new HostLinkDevice("COM5", 115200, Parity.None, 8, StopBits.One);

            //SemaphoreSlim semaphoreSlim_Comm2 = new SemaphoreSlim(1, 1);

            //plc2.Connect();

            //var Task3 = Task.Run(async () =>
            //{
            //    await semaphoreSlim_Comm2.WaitAsync();

            //    Stopwatch stopwatch2 = new Stopwatch();

            //    stopwatch2.Start();

            //    var list1 = await plc2.ReadCIOBitAsync(100, 0, 3);

            //    stopwatch2.Stop();

            //    Console.WriteLine("Read CIOBit:");
            //    foreach (var item in list1)
            //    {
            //        Console.WriteLine(item);
            //    }

            //    Console.WriteLine($"花费时间：{stopwatch2.ElapsedMilliseconds}ms");

            //    semaphoreSlim_Comm2.Release();
            //});

            //var Task4 = Task.Run(async () =>
            //{
            //    await semaphoreSlim_Comm2.WaitAsync();

            //    Stopwatch stopwatch2 = new Stopwatch();

            //    stopwatch2.Start();

            //    var list1 = await plc2.ReadWRBitAsync(0, 0, 3);

            //    stopwatch2.Stop();

            //    Console.WriteLine("Read WRBit:");
            //    foreach (var item in list1)
            //    {
            //        Console.WriteLine(item);
            //    }

            //    Console.WriteLine($"花费时间：{stopwatch2.ElapsedMilliseconds}ms");

            //    semaphoreSlim_Comm2.Release();
            //});

            //var Task5 = Task.Run(async () =>
            //{
            //    await semaphoreSlim_Comm2.WaitAsync();

            //    Stopwatch stopwatch2 = new Stopwatch();

            //    stopwatch2.Start();

            //    var list1 = await plc2.WriteWRBitAsync(0, 0, 3, "000100");

            //    stopwatch2.Stop();

            //    Console.WriteLine("Write WRBit:");
            //    Console.WriteLine(list1);

            //    Console.WriteLine($"花费时间：{stopwatch2.ElapsedMilliseconds}ms");

            //    semaphoreSlim_Comm2.Release();
            //});

            //await Task.WhenAll(Task3, Task4, Task5);

            //plc2.Disconnect();
            //semaphoreSlim_Comm2.Dispose();

            #endregion


            Console.ReadKey();
        }
    }
}
